package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements INavService {
    @Override
    public List<NavModel> getList() {
        NavDao navDao = new NavImpl();
        Jedis jedis = new Jedis();
        List<NavModel> list = new ArrayList<>();
        boolean isHave = jedis.exists("href");
        if (isHave) {
            List<String> hrefs = jedis.lrange("href", 0, -1);
            List<String> titles = jedis.lrange("title", 0, -1);
            for (int i = 0; i < hrefs.size(); i++) {
                NavModel model = new NavModel();
                model.setHref(hrefs.get(i));
                model.setName(titles.get(i));
                list.add(model);
            }
            System.out.println("导航数据来源于redis");

        } else {
            list = navDao.getList();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("href", list.get(i).getHref());
                jedis.expire("href", 30);
                jedis.rpush("title", list.get(i).getName());
                jedis.expire("title", 30);
            }
            System.out.println("导航数据来源于数据库");
        }

        return list;
    }
}
